
// 1.simple
var buf = new Buffer(256)
len = buf.write('www.ywd.me')
console.log('1.Simple ----------------------------')
console.log('Byte count:' + len)
console.log()

// 2.toString
buf = new Buffer(26)
for (var i = 0; i < 26; i++) {
    buf[i] = i + 97;
}

console.log('2.ToString --------------------------')
console.log(buf.toString('ascii'))
console.log(buf.toString('ascii', 0 , 5))
console.log(buf.toString('utf8', 0, 5))
console.log(buf.toString(undefined, 0 , 5))
console.log()

// 3.toJson
buf = new Buffer('yewenda')
var json = buf.toJSON(buf)
console.log('3.ToJSON ----------------------------')
console.log(json)
console.log()

// 4.concat
var buffer1 = new Buffer('菜鸟教程 ');
var buffer2 = new Buffer('www.runoob.com');
var buffer3 = Buffer.concat([buffer1,buffer2]);
console.log('4.concat ----------------------------')
console.log(buffer3.toString());
console.log()

// 5.compare
var buffer1 = new Buffer('ABC');
var buffer2 = new Buffer('ABCD');
var result = buffer1.compare(buffer2);

console.log('5.compare ---------------------------')
console.log(result)
if(result < 0) {
   console.log(buffer1 + " 在 " + buffer2 + "之前");
}else if(result == 0){
   console.log(buffer1 + " 与 " + buffer2 + "相同");
}else {
   console.log(buffer1 + " 在 " + buffer2 + "之后");
}
console.log()

// 6.copy
var buffer1 = new Buffer('ABC');
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log('6.copy ------------------------------')
console.log(buffer2.toString());
console.log()

// 7.slice
var buffer1 = new Buffer('runoob');
var buffer2 = buffer1.slice(0,2);
console.log('7.slice -----------------------------')
console.log(buffer2.toString());
console.log()

// 8.length
var buffer = new Buffer('www.runoob.com');
console.log('8.length ----------------------------')
console.log(buffer.length);
console.log()
